package mysql

import (
	"gorm.io/gorm"
	"shuqi-novel/userrpc/userrpc"
)

type Article struct {
	gorm.Model
	UserId  int64  `gorm:"type:int(11)"`
	Title   string `gorm:"type:varchar(50)"`
	About   string `gorm:"type:varchar(200)"`
	Type    string `gorm:"type:varchar(30)"`
	Content string `gorm:"type:text"`
}

func (a *Article) FindArticle(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(a).Error
}

func (a *Article) ArticleUpdate(db *gorm.DB) error {
	return db.Debug().Updates(a).Error
}

func (a *Article) ArticleAdd(db *gorm.DB) error {
	return db.Debug().Create(a).Error
}
func ArticleList(db *gorm.DB) ([]*userrpc.ArticleList, error) {
	var list []*userrpc.ArticleList
	err := db.Debug().Select("id as article_id",
		"user_id", "title", "about", "type", "content").
		Scopes(Paginate(1, 5)).Find(&list).Error
	return list, err
}
